package com.first.nell.components.button

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import android.widget.RelativeLayout
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentManager
import com.first.nell.R
import com.first.nell.base.BaseFormAction
import com.first.nell.databinding.ButtonFirstNellSubmitBinding

/**
<declare-styleable name="FirstNellSubmitButton">
<attr name="cwsbText" format="string" />
<attr name="cwsbTextSize" format="dimension" />
<attr name="cwsbTextColor" format="color" />
<attr name="cwsbBackgroundRes" format="reference" />
</declare-styleable>
 * */
class NextButton : RelativeLayout, BaseFormAction {

    lateinit var mFirstNellViewFirstNellBinding: ButtonFirstNellSubmitBinding
    private lateinit var vFirstNellRootFirstNellView: View

    private var cwsbTextColor: Int = 0
    private var cwsbTextSize: Float = 0F
    private var cwsbText: String? = null
    private var cwsbBackgroundRes: Int = 0
    private var cwsbIsNextArrowHidden: Boolean = true

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    @SuppressLint("Recycle")
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {
        attrs?.let {
            context?.let { cxt ->
                val array =
                    cxt.obtainStyledAttributes(attrs, R.styleable.NextButton)
                cwsbTextColor = array.getColor(
                    R.styleable.NextButton_cwsbTextColor,
                    ContextCompat.getColor(cxt, R.color.white)
                )

                cwsbText =
                    array.getString(R.styleable.NextButton_cwsbText)

                cwsbTextSize =
                    array.getDimension(
                        R.styleable.NextButton_cwsbTextSize,
                        resources.getDimensionPixelSize(R.dimen.sp_16).toFloat()
                    )

                cwsbBackgroundRes =
                    array.getResourceId(
                        R.styleable.NextButton_cwsbBackgroundRes,
                        R.drawable.first_nell_submit_checked
                    )

                cwsbIsNextArrowHidden = array.getBoolean(
                    R.styleable.NextButton_cwsbIsNextArrowHidden,
                    true
                )
                array.recycle()
            }
        }
        context?.let {
            vFirstNellRootFirstNellView = View.inflate(it, R.layout.button_first_nell_submit, this)
            mFirstNellViewFirstNellBinding =
                ButtonFirstNellSubmitBinding.bind(vFirstNellRootFirstNellView)
            initFirstNellSubviews()
        }
    }

    override fun initFirstNellSubviews() {
        try {
            mFirstNellViewFirstNellBinding.textSubmitButton.text = (cwsbText)
            mFirstNellViewFirstNellBinding.textSubmitButton.setTextColor(cwsbTextColor)
            mFirstNellViewFirstNellBinding.textSubmitButton.setTextSize(
                TypedValue.COMPLEX_UNIT_PX,
                cwsbTextSize
            )

            mFirstNellViewFirstNellBinding.textSubmitButtonBg.setBackgroundResource(cwsbBackgroundRes)

            if (cwsbIsNextArrowHidden) {
                mFirstNellViewFirstNellBinding.imageSubmitButtonNextArrow.visibility = GONE
            } else {
                mFirstNellViewFirstNellBinding.imageSubmitButtonNextArrow.visibility = VISIBLE
            }

        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

    override fun setFirstNellFragmentFirstNellManager(manager: FragmentManager) {

    }

    override fun setFirstNellOnFirstNellActionFirstNellClickFirstNellListener(listener: (Int) -> Unit) {

    }

    override fun setFirstNellTitle(title: String) {
        cwsbText = title
        mFirstNellViewFirstNellBinding.textSubmitButton.text = (cwsbText)
    }

    override fun setFirstNellDetail(detail: String) {

    }

    override fun setFirstNellContent(content: String) {

    }

    override fun setFirstNellHint(hint: String) {

    }

    override fun setFirstNellDetailFirstNellColor(detailColor: Int) {

    }

    override fun setFirstNellTitleFirstNellColor(titleColor: Int) {

    }

    fun setFirstNellStateFirstNellBan(ban: Boolean) {
        if (ban) {
            mFirstNellViewFirstNellBinding.textSubmitButtonBg.setBackgroundResource(R.drawable.first_nell_submit_ban)
        } else {
            mFirstNellViewFirstNellBinding.textSubmitButtonBg.setBackgroundResource(cwsbBackgroundRes)
        }
    }

}